﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.VoteManage
{
	public partial class VoteResult : BasePage
	{
		private LoadedValue<int> loadedIdentifier;
		/// <summary>
		/// 当前查看调查的标识符。
		/// </summary>
		public virtual int Identifier
		{
			get
			{
				if (loadedIdentifier.Loader == null)
					loadedIdentifier.Loader = () => int.Parse(Request.QueryString["VoteId"]);
				return loadedIdentifier.Value;
			}
		}

		private LoadedValue<Vote> loadedVoteContent;
		/// <summary>
		/// 调查的内容。
		/// </summary>
		public virtual Vote VoteContent
		{
			get
			{
				if (loadedVoteContent.Loader == null)
					loadedVoteContent.Loader = () => Votes.GetItem(Identifier);
				return loadedVoteContent.Value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Votes.GetUpdateRight(VoteContent))
			{
				if (!IsPostBack)
				{
					LoadList();
					Page.DataBind();
				}
			}
			else
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
		}

		protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
		{
			pager.CurrentPageIndex = e.NewPageIndex;
			LoadList();
		}

		protected void BtSearch_Click(object sender, EventArgs e)
		{
			pager.CurrentPageIndex = 1;
			LoadList();
		}

		protected void BtReload_Click(object sender, EventArgs e)
		{
			LoadList();
		}




		/// <summary>
		/// 加载列表。
		/// </summary>
		protected void LoadList()
		{
			PaginationInfo<List<VoteResults.VoteResultForList>> pageInfo =
				VoteResults.GetPaginationList(Identifier, BooleanExtensions.ParseOrNull(DdlHasUserDefinedContent.SelectedValue), TbKeyword.Text, pager.PageSize, pager.CurrentPageIndex);

			pager.RecordCount = pageInfo.RecordCount;

			List.DataSource = pageInfo.Records;
			List.DataBind();

			pager.CustomInfoHTML = Helper.GetPagerCustomInfoHtml<List<VoteResults.VoteResultForList>>(pageInfo);
		}

		protected void CtrSubmit_Click(object sender, EventArgs e)
		{
			int? resultId = IntExtensions.ParseOrNull(CtrlResultId.Value);
			if (resultId.HasValue)
			{
				bool isAward = CtrlIsAward.Checked;
				string awardContent = CtrlAwardContent.Text;

				Module.VoteResult result = new Module.VoteResult() { VoteId = Identifier, ResultId = resultId.Value, IsAward = isAward, AwardContent = awardContent };
				if (VoteResults.GetSetAwardRight(result))
				{
					VoteResults.SetAward(result);
					Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Setting));
				}
				else
					Messages.Add(new PageMessage(true, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}

			LoadList();
		}

		protected override void OnPreRender(EventArgs e)
		{
			ScriptManager.RegisterStartupScript(this, this.GetType(), "Init", "Init();", true);
			base.OnPreRender(e);
		}
	}
}
